﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
namespace QuanLyKhachSan
{
    public partial class frmThuePhong : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo .pub_ConnString );
        public frmThuePhong()
        {
            InitializeComponent();
        }

        private void frmThuePhong_Load(object sender, EventArgs e)
        {
            this.FillComboBox();
            //cbKhachHangThue.EditValue  = null ;
            
            var trutai = from tt in db.TRU_TAIs where 0 == 1 select tt;
            gridControlKhachHang.DataSource = trutai;

            this.FillGridLookUpKhachHang();
        }
        private void FillGridLookUpKhachHang()
        {
            gridLookUpEdit1.Properties.DataSource = from kh in db.KHACH_HANGs select kh;
            gridLookUpEdit1.Properties.DisplayMember = "KH_HOTEN";
            gridLookUpEdit1.Properties.ValueMember = "KH_ID";
        }
        private void FillComboBox()
        {
            List<KHACH_HANG> khachhang = (from kh in db.KHACH_HANGs orderby kh.KH_ID descending  select kh ).ToList();
            KHACH_HANG khnew = new KHACH_HANG();
            khnew.KH_CMND = "-- Thêm Mới --";
            khnew.KH_HOTEN = "-- Thêm Mới --";
            khachhang.Add(khnew);
            
          

            repositoryItemLookUpEditHoTen.DataSource = khachhang;
            repositoryItemLookUpEditHoTen.ValueMember = "KH_ID";
            repositoryItemLookUpEditHoTen.DisplayMember = "KH_HOTEN";

            repositoryItemLookUpEditCMND.DataSource = khachhang;
            repositoryItemLookUpEditCMND.ValueMember = "KH_ID";
            repositoryItemLookUpEditCMND.DisplayMember = "KH_CMND";
           

        }
       
       
        void load_form_ThuePhong(object sender, EventArgs e)
        {
            this.frmThuePhong_Load(this, EventArgs.Empty);
        }

        private void gridViewKhachHang_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {

            ColumnView view = sender as ColumnView;
            GridColumn columnHoTen = view.Columns[1];
            GridColumn columnCMND = view.Columns[2];
            int hoten = (int)view.GetRowCellValue(e.RowHandle, columnHoTen);
            int cmnd = (int)view.GetRowCellValue(e.RowHandle, columnCMND);
            //String cmnd = (String)view.GetRowCellValue(e.RowHandle, columnCMND);
            int a = view.FocusedRowHandle;
            //if (hoten == null || cmnd == null)
            //{
           

            //if (cmnd == null)
            //{
            //    e.Valid = false;
               
            //}
                //else
                //{
                //    hoten = cmnd;
                //}
          //  }
            if (hoten == 0 || cmnd  == 0)
            {
                frmKhachHang frmKH = new frmKhachHang();
                //frmKH.load_form_ThuePhong += new EventHandler(load_form_ThuePhong);
                frmKH.ShowDialog();
            }
            else
            {
                int ID = hoten;
                for (int i = 0; i < gridViewKhachHang.RowCount; i++)
                {
                    int test = int.Parse( view.GetRowCellValue(i, columnHoTen).ToString() );
                    if (ID == test && i != a)
                    {
                        e.Valid = false;
                        view.SetColumnError(columnCMND, "Khách Hàng đã tồn tại.");
                        view.SetColumnError(columnHoTen, "Khách Hàng đã tồn tại.");
                    }    
                }
            }
        }

        private void gridViewKhachHang_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

      

        private void gridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookUpEdit1View.GetRowCellValue(gridLookUpEdit1View.FocusedRowHandle, "KH_CMND") != null)
            {
                textEdit1.Text = gridLookUpEdit1View.GetRowCellValue(gridLookUpEdit1View.FocusedRowHandle, "KH_CMND").ToString();
            }
            else
            {
                textEdit1.Text = "";
            }
        }
        private void btLuu_Click(object sender, EventArgs e)
        {
            MessageBox.Show(dateEditBatDau .EditValue.ToString());
        }

        private void timeEditBatDau_EditValueChanged(object sender, EventArgs e)
        {
            //timeEditBatDau.EditValue = dateEditBatDau.EditValue;
        }
    }
}